<?php

require_once '../config.php';
require_once '../functions.php';


books_get_current_user();

function add_users()
{
    if (empty($_POST['email'])) {
        $GLOBALS['message'] = "请填写邮箱";
        $GLOBALS['success'] = false;
        return;
    }
    if (empty($_POST['slug'])) {
        $GLOBALS['message'] = "请填写别名";
        $GLOBALS['success'] = false;
        return;
    }
    if (empty($_POST['bio'])) {
        $GLOBALS['message'] = "请填写昵称";
        $GLOBALS['success'] = false;
        return;
    }
    if (empty($_POST['password'])) {
        $GLOBALS['message'] = "请填写密码";
        $GLOBALS['success'] = false;
        return;
    }
  
    //接收并保存
    $email = $_POST['email'];
    $slug = $_POST['slug'];
    $bio = $_POST['bio'];
    $password = $_POST['password'];
    $guanli = $_POST['guanli'];
  
    $rows = book_execute("insert into users (id,slug,email,password,bio,guanli)values (null,'{$slug}','{$email}','{$password}','{$bio}','{$guanli}');");
    $GLOBALS['success'] = $rows > 0;
    $GLOBALS['message'] = $rows <= 0 ? '添加失败！' : '添加成功！';
}


//编辑功能
function edit_users()
{
    global $current_edit_users;
    $id = $current_edit_users['id'];
    $slug = empty($_POST['slug']) ? $current_edit_users['slug'] : $_POST['slug'];
    $current_edit_users['email'] = $slug;
    $email = empty($_POST['email']) ? $current_edit_users['email'] : $_POST['email'];
    $current_edit_users['email'] = $email;

    $bio = empty($_POST['bio']) ? $current_edit_users['bio'] : $_POST['bio'];
    $current_edit_users['bio'] = $bio;

    $guanli = empty($_POST['guanli']) ? $current_edit_users['guanli'] : $_POST['guanli'];
    $current_edit_users['guanli'] = $guanli;
    $password = empty($_POST['password']) ? $current_edit_users['password'] : $_POST['password'];
    $current_edit_users['password'] = $password;
    $rows = book_execute("update users set slug ='{$slug}',email = '{$email}', bio = '{$bio}',password='{$password}',guanli='{$guanli}'  where id = {$id} ");
    $GLOBALS['success'] = $rows > 0;
    $GLOBALS['success'] = $rows <= 0 ? '更新成功！':'更新失败';

    header('Location: /admin/user.php');
}
  if (empty($_GET['id'])) {
      //  添加
      if ($_SERVER['REQUEST_METHOD']==='POST') {
          add_users();
      }
  } else {
      $current_edit_users =book_fetch_one('select * from users where id ='. $_GET['id']);
      if ($_SERVER['REQUEST_METHOD']==='POST') {
          edit_users();
      }
  }




// 查询全部数据分类
$lists = book_fetch_all('select * from users;');


?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Users &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
    <?php include 'inc/navbar_admin.php'; ?>

    <div class="container-fluid">
      <div class="page-title">
        <h1>用户</h1>
      </div>
     <?php if (isset($message)): ?>
     <?php if ($success):?>
              <!-- 有错误信息时展示 -->
      <div class="alert alert-success">
        <strong>成功！</strong><?php echo $message;?>
      </div>
    <?php else:?>
       <div class="alert alert-danger">
        <strong>错误！</strong><?php echo $message;?>
      </div>
     <?php endif ?>
     <?php endif ?>
      <div class="row">
        <div class="col-md-4">
        <?php if (isset($current_edit_users)): ?>
           <form action="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $current_edit_users['id']; ?>" method="post">
            <h2>编辑 <?php echo $current_edit_users['email'] ?></h2>
            <div class="form-group">
              <label for="email">邮箱</label>
              <input id="email" class="form-control" name="email" type="email" placeholder="邮箱" value="<?php echo $current_edit_users['email'];?> " >
            </div>
            <div class="form-group">
              <label for="slug">别名</label>
              <input id="slug" class="form-control" name="slug" type="text" placeholder="slug" value="<?php echo $current_edit_users['slug'];?>" >
            </div>
            <div class="form-group">
              <label for="bio">昵称</label>
              <input id="bio" class="form-control" name="bio" type="text" placeholder="昵称" value="<?php echo $current_edit_users['bio'];?> ">
            </div>
            <div class="form-group">
              <label for="guanli">类型</label>
              <select name="guanli" class="form-control input-sm">
                    <option selected="selected" value="<?php echo $current_edit_users['guanli']=="1"?"管理员":"用户";?>"><?php echo $current_edit_users['guanli']=="1"?"管理员":"用户";?></option>
                    <option value="1">管理员</option>
                    <option value="2">用户</option>
              </select>
            </div>
            <div class="form-group">
              <label for="password">密码</label>
              <input id="password" class="form-control" name="password" type="text" placeholder="密码" value="<?php echo $current_edit_users['password'];?> ">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">保存</button>
            </div>
          </form>
        <?php else: ?>
           <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <h2>添加数据</h2>
            <div class="form-group">
              <label for="email">邮箱</label>
              <input id="email" class="form-control" name="email" type="email" placeholder="邮箱" >
            </div>
            <div class="form-group">
              <label for="slug">别名</label>
              <input id="slug" class="form-control" name="slug" type="text" placeholder="slug">
            </div>
            <div class="form-group">
              <label for="bio">昵称</label>
              <input id="bio" class="form-control" name="bio" type="text" placeholder="昵称">
            </div>
            <div class="form-group">
              <label for="bio">类型</label>
              <select name="guanli" class="form-control input-sm">
                    <option value="all">用户类型</option>
                    <option value="1">管理员</option>
                    <option value="2">用户</option>
                    </select>
            </div>



            
            <div class="form-group">
              <label for="password">密码</label>
              <input id="password" class="form-control" name="password" type="text" placeholder="密码">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">添加</button>
            </div>
          </form>
        <?php endif ?>
         
        </div>
        <div class="col-md-8">
          <div class="page-action">
            <!-- show when multiple checked -->
            <a id="btn_delete" class="btn btn-danger btn-sm" href="/admin/users-delete.php" style="display: none">批量删除</a>
          </div>
          <table class="table table-striped table-bordered table-hover">
            <thead>
               <tr>
                <th class="text-center" width="80">头像</th>
                <th>邮箱</th>
                <th>别名</th>
                <th>昵称</th>
                <th>密码</th>
                <th>类型</th>
                <th class="text-center" width="100">操作</th>
              </tr>
            </thead>
            <tbody>
            <?php foreach ($lists as $item): ?>
              <tr>

                <td class="text-center"><img class="avatar" src="<?php echo empty($item['avatar']) ? '/static/assets/img/default.png' : $item['avatar']; ?>"></td>
                <td><?php echo $item['email']; ?></td>
                <td><?php echo $item['slug']; ?></td>
                <td><?php echo $item['bio']; ?></td>
                <td><?php echo $item['password'];?></td>
                <td><?php echo $item['guanli']=='1'?"管理员":"用户";?></td>
                <td class="text-center">
                  <a href="/admin/user.php?id=<?php echo $item['id']; ?>" class="btn btn-default btn-xs">编辑</a>
                  <a href="/admin/user-delete.php?id=<?php echo $item['id'] ?>" class="btn btn-danger btn-xs">删除</a>
                </td>
              </tr>
            <?php endforeach ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
  <?php $current_page = 'user'; ?>
  <?php include 'inc/sidebar.php'; ?> 
  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>NProgress.done()</script>
</body>
</html>
